clear
est clear


cap mat define migrationfigure=J(105,10,.)
		local row=1

	use "$input/full_geoid_pair_panel.dta"
	
	replace migration=migration/(tot_pop/100000)

	//Make sure only include states that have a storm in at least one county at least once in our sample years
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	//Create future pooled migration - total migrants in the subsequent five years
	sort geoid_pair move_year
	g post_storm=1 if storm==1|storm[_n-1]==1&geoid_pair==geoid_pair[_n-1]|storm[_n-2]==1&geoid_pair==geoid_pair[_n-2]|storm[_n-3]==1&geoid_pair==geoid_pair[_n-3]|storm[_n-4]==1&geoid_pair==geoid_pair[_n-4]|storm[_n-5]==1&geoid_pair==geoid_pair[_n-5]
	mvencode post_storm, mv(0)
	
	//Collapse to total migration from a county (not looking at paired county to county migration, just all migration out of a county)
	collapse (sum) migration (mean) share_approved totalapprovedihpamount,by(from_geoid move_year storm post_storm state_fips)
	
	//Create IHS transformations
	ihstrans(migration)
	
	
	//Create lags of storms (was there a storm in t-1 years)

			
	label var post_storm "1(0-5 yrs post storm)"
	label var storm "1(Storm year)"
	sort from_geoid move_year
	
	replace totalap=totalap/1000000
	g highdamage=(totalapp>10) if totalapp!=.
	mvencode highdamage, mv(0) o
	
	rename from_geoid fips
	
	preserve
		keep if highdamage==1
		export delimited "$figdat/highdamage_10mm.csv", replace
	restore
		
		drop highdamage*
		g highdamage=(totalapp>20) if totalapp!=.
		mvencode highdamage, mv(0) o
	preserve
		keep if highdamage==1
		export delimited "$figdat/highdamage_20mm.csv", replace
	restore	
		
		drop highdamage*
		g highdamage=(totalapp>40) if totalapp!=.
		mvencode highdamage, mv(0) o
	
	preserve
		keep if highdamage==1
		export delimited "$figdat/highdamage_40mm.csv", replace
	restore
		
		drop highdamage*
		g highdamage=(totalapp>80) if totalapp!=.
		mvencode highdamage, mv(0) o
	
	preserve
		keep if highdamage==1
		export delimited "$figdat/highdamage_80mm.csv", replace
	restore	
		
		
		drop highdamage*
		g highdamage=(totalapp>160) if totalapp!=.
		mvencode highdamage, mv(0) o
		
	preserve
		keep if highdamage==1
		export delimited "$figdat/highdamage_160mm.csv", replace
	restore		
			
			